package com.lancoo.answer.view.xgSaResultView.adapter

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.lancoo.answer.R
import com.lancoo.answer.databinding.EvXgAdapterAnswersheetChildBinding
import com.lancoo.answer.model.bean.CognitiveBean
import com.lancoo.answer.model.bean.XgSaBean
import com.lancoo.answer.model.test.Item
import com.lancoo.answer.util.DensityUtils
import com.lancoo.answer.view.base.XBaseRecyclerAdapter
import com.lancoo.answer.view.base.XBaseRecyclerHolder

class XgSaResultAnswerSheetChildAdapter(
    val context: Context,
    val cognitiveBean: CognitiveBean
) : XBaseRecyclerAdapter<EvXgAdapterAnswersheetChildBinding>() {
    private var sortIndexType = ""

    override fun onBindingView(viewGroup: ViewGroup?): EvXgAdapterAnswersheetChildBinding {
        return EvXgAdapterAnswersheetChildBinding.inflate(
            LayoutInflater.from(
                viewGroup!!.context
            ), viewGroup, false
        )
    }

    override fun onBindingData(
        holder: XBaseRecyclerHolder<EvXgAdapterAnswersheetChildBinding>,
        position: Int
    ) {
        sortIndexType = cognitiveBean.TypeList!![0].QuesList!![0].ChildList!![0].SortIndexType!!
        var indexA = position_return_indexA(cognitiveBean, position)
        var indexB = position_return_indexB(cognitiveBean, position)
        var indexC = position_return_indexC(cognitiveBean, position)
        //适配翻译填空一题多空
        if (cognitiveBean.TypeList!![0].QuesList!![indexA].GenreID == "22") {
            indexA = getIndexA(cognitiveBean, position)
            cognitiveBean.TypeList!![0].QuesList!![indexA].ChildList!!.forEach {
                if (it.ItemList!!.size > 1) {
                    indexB = 0
                    indexC = 0
                }
            }
        }

        val isAnswer = cognitiveBean.TypeList!![0].QuesList!![indexA].ChildList!![indexB].ItemList!![indexC].IsAnswer
        val state = cognitiveBean.TypeList!![0].QuesList!![indexA].ChildList!![indexB].ItemList!![indexC].State
        val scoreRate =
            cognitiveBean.TypeList!![0].QuesList!![indexA].ChildList!![indexB].ItemList!![indexC].ScoreRate
        var sortIndex =
            cognitiveBean.TypeList!![0].QuesList!![indexA].ChildList!![indexB].ItemList!![indexC].SortIndex

        holder.viewBinding.TvSortIndex.text = sortIndex
        if (sortIndex!!.contains("-")) {
//            val linearParams = holder.viewBinding.TvSortIndex.layoutParams
//            linearParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
//            linearParams.height = DensityUtils.dp2px(context,32f)
//            holder.viewBinding.TvSortIndex.layoutParams = linearParams
            if (state == 0|| state == -1) {
                if (isAnswer) {
                    holder.viewBinding.TvSortIndex.setBackgroundResource(R.drawable.button_round_blue)
                    holder.viewBinding.TvSortIndex.setTextColor(Color.WHITE)
                } else {
                    holder.viewBinding.TvSortIndex.setBackgroundResource(R.drawable.button_round_white)
                    holder.viewBinding.TvSortIndex.setTextColor(Color.parseColor("#333333"))
                }
            } else {
                if (scoreRate >= 0.6) {
                    holder.viewBinding.TvSortIndex.setBackgroundResource(R.drawable.button_round_green)
                    holder.viewBinding.TvSortIndex.setTextColor(Color.WHITE)
                } else {
                    holder.viewBinding.TvSortIndex.setBackgroundResource(R.drawable.button_round_red)
                    holder.viewBinding.TvSortIndex.setTextColor(Color.WHITE)
                }
            }
        } else {
//            val linearParams = holder.viewBinding.TvSortIndex.layoutParams
//            linearParams.width = DensityUtils.dp2px(context,30f)
//            linearParams.height = DensityUtils.dp2px(context,30f)
//            holder.viewBinding.TvSortIndex.layoutParams = linearParams
            if (state == 0|| state == -1) {
                if (isAnswer) {
                    holder.viewBinding.TvSortIndex.setBackgroundResource(R.drawable.button_round_blue)
                    holder.viewBinding.TvSortIndex.setTextColor(Color.WHITE)
                } else {
                    holder.viewBinding.TvSortIndex.setBackgroundResource(R.drawable.button_round_white)
                    holder.viewBinding.TvSortIndex.setTextColor(Color.parseColor("#333333"))
                }
            } else {
                if (scoreRate >= 0.6) {
                    holder.viewBinding.TvSortIndex.setBackgroundResource(R.drawable.button_round_green)
                    holder.viewBinding.TvSortIndex.setTextColor(Color.WHITE)
                } else {
                    holder.viewBinding.TvSortIndex.setBackgroundResource(R.drawable.button_round_red)
                    holder.viewBinding.TvSortIndex.setTextColor(Color.WHITE)
                }
            }
        }


        holder.viewBinding.TvSortIndex.setOnClickListener {
            clickChilidListener!!.onClick((position + 1).toString())
        }
    }

    override fun getItemCount(): Int {
        var itemCount = 0
        var quesListSize = cognitiveBean.TypeList!![0].QuesList!!.size
        val x =1
        for (a in 0 until quesListSize) {
            val quesChildNum = cognitiveBean.TypeList!![0].QuesList!![a].CustomModel?.QuesChildNum
            itemCount += quesChildNum!!
        }
        return itemCount
    }

    private fun getIndexA(cognitiveBean: CognitiveBean, position: Int): Int {
        var startCount = 0
        var endCount = 0
        var quesListSize = cognitiveBean!!.TypeList!![0].QuesList!!.size
        for (a in 0 until quesListSize) {
            startCount = endCount
            endCount += cognitiveBean!!.TypeList!![0].QuesList!![a].CustomModel!!.QuesChildNum!!
            if (position in startCount until endCount) {
                return a
            }
        }
        return startCount

    }

    private fun position_return_indexA(cognitiveBean: CognitiveBean, position: Int): Int {
        var postionX = -1
        var index_A = 0
        var quesListSize = cognitiveBean!!.TypeList!![0].QuesList!!.size
        if (sortIndexType == "0") {
            cognitiveBean!!.TypeList!![0].QuesList!!.forEachIndexed { quesIndex, xgSaBeanItem ->
                cognitiveBean!!.TypeList!![0].QuesList!![quesIndex].ChildList!!.forEachIndexed { childIndex, childSame ->
                    postionX +=1
                    if (position == postionX) {
                        index_A = quesIndex
                    }
                }
            }
        } else {
            outLoop@ for (a in 0 until quesListSize) {
                var childListSize = cognitiveBean!!.TypeList!![0].QuesList!![a].ChildList!!.size
                for (b in 0 until childListSize) {
                    val itemListSize = cognitiveBean!!.TypeList!![0].QuesList!![a].ChildList!![b].ItemList!!.size
                    for (c in 0 until itemListSize) {
                        postionX += 1
                        if (position == postionX) {
                            index_A = a
                            break@outLoop
                        }
                    }
                }
            }
        }
        return index_A
    }

    private fun position_return_indexB(cognitiveBean: CognitiveBean, position: Int): Int {
        var postionX = -1
        var index_B = 0
        var quesListSize = cognitiveBean!!.TypeList!![0].QuesList!!.size
        if (sortIndexType == "0") {
            cognitiveBean!!.TypeList!![0].QuesList!!.forEachIndexed { quesIndex, xgSaBeanItem ->
                cognitiveBean!!.TypeList!![0].QuesList!![quesIndex].ChildList!!.forEachIndexed { childIndex, childSame ->
                    postionX +=1
                    if (position == postionX) {
                        index_B = childIndex
                    }
                }
            }
        } else {
            outLoop@ for (a in 0 until quesListSize) {
                var childListSize = cognitiveBean!!.TypeList!![0].QuesList!![a].ChildList!!.size
                for (b in 0 until childListSize) {
                    val itemListSize = cognitiveBean!!.TypeList!![0].QuesList!![a].ChildList!![b].ItemList!!.size
                    for (c in 0 until itemListSize) {
                        postionX += 1
                        if (position == postionX) {
                            index_B = b
                            break@outLoop
                        }
                    }
                }
            }
        }
        return index_B
    }

    private fun position_return_indexC(cognitiveBean: CognitiveBean, position: Int): Int {
        var postionX = -1
        var index_C = 0
        var quesListSize = cognitiveBean!!.TypeList!![0].QuesList!!.size
        if (sortIndexType == "0") {
            index_C = 0
        } else {
            outLoop@ for (a in 0 until quesListSize) {
                var childListSize = cognitiveBean!!.TypeList!![0].QuesList!![a].ChildList!!.size
                for (b in 0 until childListSize) {
                    val itemListSize = cognitiveBean!!.TypeList!![0].QuesList!![a].ChildList!![b].ItemList!!.size
                    for (c in 0 until itemListSize) {
                        postionX += 1
                        if (position == postionX) {
                            index_C = c
                            break@outLoop
                        }
                    }
                }
            }
        }
        return index_C
    }

    interface OnClickChildListener {
        fun onClick(sortIndex: String)
    }

    private var clickChilidListener: OnClickChildListener? = null

    fun setClickChildListener(clickChilidListener: OnClickChildListener?) {
        this.clickChilidListener = clickChilidListener
    }

}